package com.kcsj.util;
//201902104058胡庆阳
public class Pagination {
    //每页的对象(元组)数
    private int pageSize;
    //页号
    private int pageNo;
    //对象(元组)总数
    private int totalNum = 10;

    public Pagination() {
    }

    public Pagination(int pageSize, int pageNo) {
        //防止错误赋值
        if (pageSize > 0) {
            this.pageSize = pageSize;
        } else {
            this.pageSize = 5;
        }
        if (pageNo > 0) {
            this.pageNo = pageNo;
        } else {
            this.pageNo = 1;
        }
    }

    //返回LIMIT子句
    public String toLimitClause(int totalNum) {
        //响前台提供元组总数(totalNum为0不是错误)
        this.totalNum = totalNum;
        //偏移量
        int offset = (this.pageNo - 1) * this.pageSize;
        //输出LIMIT子句，注意关键字两端的空格
        return " LIMIT " + offset + "," + this.pageSize;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPageNo() {
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public int getTotalNum() {
        return totalNum;
    }

    public void setTotalNum(int totalNum) {
        this.totalNum = totalNum;
    }

    @Override
    public String toString() {
        return "Pagination{" +
                "pageSize=" + pageSize +
                ", pageNo=" + pageNo +
                ", totalNum=" + totalNum +
                '}';
    }
}
